package commons

//在一段排好序的数组nums中，查找num是否存在
func BinarySearch(nums []int, num int) bool {
	return binarySearch(nums, num, 0, len(nums)-1)
}

func binarySearch(nums []int, num int, s, e int) bool {
	if s > e {
		return false
	}
	if s == e {
		if nums[s] == num {
			return true
		} else {
			return false
		}
	}
	m := (s + e) / 2
	//fmt.Println(nums[m])
	if nums[m] == num {
		return true
	}
	if nums[m] > num {
		return binarySearch(nums, num, s, m-1)
	} else {
		return binarySearch(nums, num, m+1, e)
	}
}
